<template>
  <div class="directorys-add">
    <el-dialog
      :title="title"
      :visible="dialogVisible"
      width="400px"
      @close="closeDialog"
    >
      <el-form ref="formRef" size="small" label-width="80px" :model="formModel" :rules="fromRules">
        <el-form-item v-if="!$route.query.id" label="所属学科" prop="subjectID">
          <el-select v-model="formModel.subjectID" style="width:100%">
            <el-option
              v-for="item in subjectOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" prop="directoryName">
          <el-input v-model="formModel.directoryName" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="confirm()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { simple as subjectList } from '../../api/hmmm/subjects'
import { add, update } from '../../api/hmmm/directorys'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    currDirectory: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      formModel: {
        id: null,
        subjectID: null,
        directoryName: null
      },
      fromRules: {
        subjectID: [
          { required: true, message: '请选择', trigger: 'blur' }
        ],
        directoryName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' }
        ]
      },
      subjectOptions: []
    }
  },
  computed: {
    title() {
      return this.formModel.id ? '修改目录' : '新增目录'
    }
  },
  created() {
    this.getSubjectList()
  },
  methods: {
    // 修改
    open() {
      this.$emit('update:dialogVisible', true)
      if (this.currDirectory.id) {
        const { id, subjectID, directoryName } = this.currDirectory
        this.formModel = { id, subjectID, directoryName }
      } else {
        this.formModel = {
          id: null,
          subjectID: +this.$route.query.id || null,
          directoryName: null
        }
      }
    },
    // 关闭弹层
    closeDialog() {
      this.$emit('update:dialogVisible', false)
      this.formModel = {
        id: null,
        subjectID: null,
        directoryName: null
      }
      this.$refs.formRef.resetFields()
    },
    // 获取学科下拉列表
    async getSubjectList() {
      const { data } = await subjectList()
      this.subjectOptions = data
      // console.log(res)
    },
    // 添加/修改
    confirm() {
      this.$refs.formRef.validate(async(valid) => {
        if (valid) {
          if (!this.formModel.id) {
            // 新增
            await add(this.formModel)
          } else {
            // 修改
            this.formModel.id = this.currDirectory.id
            await update(this.formModel)
          }
          this.$message.success('操作成功')
          this.closeDialog()
          this.$emit('updateList')
        }
      })
    }
  }
}
</script>

<style scoped lang='scss'></style>
